import 'package:dodoclub/pages/comm/method_bridge.dart';
import 'package:dodoclub/widgets/tile_item.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'dart:convert';

class IndexPage extends StatefulWidget {
  const IndexPage({Key? key}) : super(key: key);

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  late ThemeData theme;
  late Future<Map<String, dynamic>> _futureResult; // 假设你返回的是字符串


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _futureResult = MethodBridge.getInstance().request("room/live/list",
        {"roomType": "1", "page": "0", "size": "30", "needOfflineRoom": "1"});
  }

  @override
  Widget build(BuildContext context) {
    theme = Theme.of(context);

    return Scaffold(
        backgroundColor: theme.scaffoldBackgroundColor,
        appBar: AppBar(
          scrolledUnderElevation: 0.0,
          title: const Text('Index'),
        ),
        body: FutureBuilder<Map<String, dynamic>>(
          future: _futureResult, // 提供一个Future对象
          builder: (BuildContext context,
              AsyncSnapshot<Map<String, dynamic>> snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.none:
              case ConnectionState.waiting:
                return getLoadingText();
              case ConnectionState.active:
                return getLoadingText(); // 或者其他UI表示正在加载
              case ConnectionState.done:
                if (snapshot.hasError) {
                  return Text('Error: ${snapshot.error}');
                } else {
                  // return Text('Data: ${snapshot.data}');
                  List<Map<String, dynamic>> items =
                      (snapshot.data?["obj"]?["result"] ?? [])
                          .cast<Map<String, dynamic>>();
                  ;
                  return getDefaultUI(items);
                }
            }
          },
        ));
  }

  Widget getLoadingText() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            'Loading...',
            style: TextStyle(fontSize: 24),
            textAlign: TextAlign.center, // 对于单行文本，这通常是可选的
          ),
        ],
      ),
    );
  }

  Widget getDefaultUI(List<Map<String, dynamic?>> items) {
    MethodBridge.getInstance().print("items: ${items}");
    return SafeArea(
      child: SingleChildScrollView(
        physics: const BouncingScrollPhysics(),
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: StaggeredGrid.count(
            crossAxisCount: 4,
            mainAxisSpacing: 4,
            crossAxisSpacing: 4,
            children: items.map((item) {
              return StaggeredGridTile.count(
                crossAxisCellCount: 2,
                mainAxisCellCount: 2,
                child: Tile(index: 0, model: item),
              );
            }).toList(),
          ),
        ),
      ),
    );
  }
/*
  StaggeredGridTile.count(
                crossAxisCellCount: 2,
                mainAxisCellCount: 2,
                child: Tile(index: 0),
              )
  * */
}
